package com.qingcheng.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.pojo.order.Order;
import com.qingcheng.pojo.order.OrderItem;
import com.qingcheng.service.goods.SkuService;
import com.qingcheng.service.order.OrderItemService;
import com.qingcheng.service.order.OrderService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/find")
public class Controller {


    @Reference
    private OrderItemService orderItemService;

    @Reference
    private OrderService orderService;

    @Reference
    private SkuService skuService;

    @GetMapping("/findOrder")
    public List<Map<String, Object>> findOrder(String page, String size) {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();

        //   List<Order> orders = orderService.findByUsername(username);
        Map searchMap = new HashMap();
        searchMap.put("username", username);
        if (page == null) {
            page = "1";
        }
        if (size == null) {
            size = "4";
        }
        PageResult<Order> pageResult = orderService.findPage(searchMap, Integer.parseInt(page), Integer.parseInt(size));

        List<Order> orders = pageResult.getRows();


        List<Map<String, Object>> orderList = new ArrayList<>();

        for (Order order : orders) {

            Map map = new HashMap();

            List<OrderItem> orderItemList = orderItemService.findByOrderId(order.getId());

            for (OrderItem orderItem : orderItemList) {
                Sku sku = skuService.findById(orderItem.getSkuId());
                orderItem.setSpec(sku.getSpec());
            }

            map.put("orderItem", orderItemList);

            map.put("orderId", order.getId());

            map.put("order", order);

            map.put("createTime", order.getCreateTime());

            map.put("length",orderItemList.size());

            //  map.put("page",pageResult.getPage());
            map.put("total", pageResult.getTotal());//总记录数

          //  map.put("pages", pageResult.getPages());//总页数

            map.put("pages", pageResult.getPages());
            map.put("pageNum", Integer.parseInt(page));//当前页码

            map.put("size", Integer.parseInt(size));//当前页显示条数

            orderList.add(map);
        }

        return orderList;

    }

//    @GetMapping("/orderPage")
//    public Map<String,Object> orderPage(String page,String size){
//
//        String username = SecurityContextHolder.getContext().getAuthentication().getName();
//
//        //   List<Order> orders = orderService.findByUsername(username);
//        Map searchMap=new HashMap();
//        searchMap.put("username",username);
//        if (page==null){
//            page="1";
//        }
//        if (size==null){
//            size="4";
//        }
//
//        PageResult pageResult = orderService.findPage(searchMap, Integer.parseInt(page), Integer.parseInt(size));
//
//        Map map=new HashMap();
//        map.put("pageHelper",pageResult.getPage());
//
//        return map;
//    }

    /**
     * 待付款
     *
     * @return
     */
    @GetMapping("/unPay")
    public List<Map<String, Object>> unPay() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();

        //   List<Order> orders = orderService.findByUsername(username);
        Map searchMap = new HashMap();
        searchMap.put("username", username);
        searchMap.put("orderStatus", "0");
        List<Order> orderList = orderService.findList(searchMap);

        List<Map<String, Object>> orderLists = new ArrayList<>();

        for (Order order : orderList) {

            Map map = new HashMap();

            List<OrderItem> orderItemList = orderItemService.findByOrderId(order.getId());

            for (OrderItem orderItem : orderItemList) {
                Sku sku = skuService.findById(orderItem.getSkuId());
                orderItem.setSpec(sku.getSpec());
            }

            map.put("orderItem", orderItemList);

            map.put("orderId", order.getId());

            map.put("order", order);

            map.put("createTime", order.getCreateTime());

            //  map.put("page",pageResult.getPage());

            orderLists.add(map);
        }

        return orderLists;

    }

    @GetMapping("/unSend")
    public List<Map<String, Object>> unSend() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();

        //   List<Order> orders = orderService.findByUsername(username);
        Map searchMap = new HashMap();
        searchMap.put("username", username);
        searchMap.put("orderStatus", "1");
        List<Order> orderList = orderService.findList(searchMap);

        List<Map<String, Object>> orderLists = new ArrayList<>();

        for (Order order : orderList) {

            Map map = new HashMap();

            List<OrderItem> orderItemList = orderItemService.findByOrderId(order.getId());

            for (OrderItem orderItem : orderItemList) {
                Sku sku = skuService.findById(orderItem.getSkuId());
                orderItem.setSpec(sku.getSpec());
            }

            map.put("orderItem", orderItemList);

            map.put("orderId", order.getId());

            map.put("order", order);

            map.put("createTime", order.getCreateTime());

            //  map.put("page",pageResult.getPage());

            orderLists.add(map);
        }

        return orderLists;

    }


    /**
     * 待收货
     *
     * @return
     */
    @GetMapping("/unRecieve")
    public List<Map<String, Object>> unRecieve() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();

        //   List<Order> orders = orderService.findByUsername(username);
        Map searchMap = new HashMap();
        searchMap.put("username", username);
        searchMap.put("orderStatus", "2");
        List<Order> orderList = orderService.findList(searchMap);

        List<Map<String, Object>> orderLists = new ArrayList<>();

        for (Order order : orderList) {

            Map map = new HashMap();

            List<OrderItem> orderItemList = orderItemService.findByOrderId(order.getId());

            for (OrderItem orderItem : orderItemList) {
                Sku sku = skuService.findById(orderItem.getSkuId());
                orderItem.setSpec(sku.getSpec());
            }

            map.put("orderItem", orderItemList);

            map.put("orderId", order.getId());

            map.put("order", order);

            map.put("createTime", order.getCreateTime());

            //  map.put("page",pageResult.getPage());

            orderLists.add(map);
        }


        return orderLists;

    }

    /**
     * 取消订单
     * @return
     */
    @GetMapping("/cancel")
    public void cancel(String id) {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        //   List<Order> orders = orderService.findByUsername(username);
        Order order = new Order();
        order.setId(id);
        order.setOrderStatus("4");
        orderService.update(order);

    }
}

